package cn.emailChat.ai.config;

import cn.emailChat.ai.replychain.ReplyHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Comparator;
import java.util.List;
/**
 * @Description 责任链装配
 * @Author susu
 * @Date 2025/8/18
 */
@Slf4j
@Configuration
public class ReplyChainConfig {

    /** 把所有 ReplyHandler bean 按 @Order 排序后串成一条链，返回链头 */
    @Bean
    public ReplyHandler replyChainHead(List<ReplyHandler> handlers) {
        handlers.sort(Comparator.comparingInt(h -> h.getClass().getAnnotation(org.springframework.core.annotation.Order.class).value()));
        for (int i = 0; i < handlers.size() - 1; i++) {
            handlers.get(i).setNext(handlers.get(i + 1));
        }
        log.info("[ReplyChain] 已装配处理链：{} 个节点", handlers.size());
        return handlers.get(0);
    }
}
